﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_gold_topUpGold : System.Web.UI.Page
{
    db_users user = new db_users();
    db_gold gold = new db_gold();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("../inc/checkPurview.aspx"); // 此处用户权限验证
        if (!IsPostBack)
        {
            initCardID();
        }
    }

    private void initCardID() {
        DataSet ds = new DataSet();
        if (!lbl_uID.Text.Equals("编号"))
        {
            ds = gold.selectCardSell_userID(lbl_uID.Text);
            if (ds.Tables.Count != 0)
            {
                ddl_card.DataSource = ds.Tables[0].DefaultView;
                ddl_card.DataTextField = "goldcardid";
                ddl_card.DataValueField = "goldcardid";
                ddl_card.DataBind();
                ddl_card.Items.Insert(0, new ListItem("请选择卡号", ""));
                ddl_card.Items[0].Selected = true;
            }
        }
        else {
            ddl_card.DataSource = "";
            ddl_card.DataBind();
        }
    }


    protected void btn_user_Click(object sender, EventArgs e)
    {
        string name = "";
        if (tb_name.Text.Trim().Equals(""))
        {
            Response.Write("<script language=javascript>alert('请输入用户名');</script>");
            res();
            tb_name.Focus();
            return;
        }
        else
        {
            name = tb_name.Text.Trim();
        }
        DataSet ds = new DataSet();
        ds = user.selectUserONE_username(name);
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script language=javascript>alert('用户不存在');</script>");
            tb_name.Focus();
            return;
        }
        else
        {
            lbl_uName.Text = ds.Tables[0].Rows[0]["userName"].ToString();
            lbl_uID.Text = ds.Tables[0].Rows[0]["userID"].ToString();
            initCardID();
        }
    }
    protected void btn_goldcard_Click(object sender, EventArgs e)
    {
        string cardID = "";
        if (ddl_card.SelectedValue.Equals("请选择卡号") || ddl_card.SelectedValue.Equals(""))
        {
            Response.Write("<script language=javascript>alert('请选择卡号');</script>");
            lbl_gold.Text = "金币值";
            lbl_id.Text = "卡号";
        }
        else
        {
            cardID = ddl_card.SelectedValue;
            DataSet ds = new DataSet();
            ds = gold.selectCardGold(cardID);
            lbl_gold.Text =  ds.Tables[0].Rows[0]["gold"].ToString();
            lbl_id.Text = ds.Tables[0].Rows[0]["goldcardid"].ToString();
            
        }
    }

    private void res()
    {
        lbl_uID.Text = "编号";
        lbl_gold.Text = "金币值";
        lbl_uName.Text = "用户名";
        lbl_id.Text = "卡号";
        tb_name.Text = "";
        initCardID();
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        //Response.Write("<script language=javascript>alert('请选择卡号');</script>");
        string cardID = lbl_id.Text.Trim();
        string cardGold=lbl_gold.Text.Trim();
        string uID=lbl_uID.Text.Trim();
        if (cardID.Equals("卡号") || cardGold.Equals("金币值")) {
            Response.Write("<script language=javascript>alert('请选择卡号');</script>");
            return;
        }
        if (uID.Equals("编号")) {
            Response.Write("<script language=javascript>alert('请输入用户名');</script>");
            tb_name.Focus();
            return;
        }
        if (gold.isCardUse(cardID))
        {
            Response.Write("<script language=javascript>alert('该卡已经被使用了！');</script>");
            return;
        }
        else
        {
            if (gold.topUpGoldCard(uID, cardGold, cardID))
            {
                Response.Write("<script language=javascript>alert('充值成功');</script>");
                res();
                return;
            }
            else
            {
                Response.Write("<script language=javascript>alert('失败');</script>");
            }
        }
    }
}